package com.imyyq.sample.rv

import android.app.Application
import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.imyyq.mvvm.base.AppBarBaseViewModel
import com.imyyq.mvvm.base.BaseModel
import com.imyyq.mvvm.utils.LogUtil
import com.imyyq.sample.BR
import com.imyyq.sample.R
import com.imyyq.sample.app.MyCommonAppBarProcessor2
import com.imyyq.sample.data.Repository
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.delay
import me.tatarka.bindingcollectionadapter2.itembindings.OnItemBindClass
import me.tatarka.bindingcollectionadapter2.map

class RecyclerViewViewModel(app: Application) : AppBarBaseViewModel<Repository, MyCommonAppBarProcessor2>(
    app
) {

    override fun onResume(owner: LifecycleOwner) {
        launch({
            delay(2000)
            mModel.getData("zijie", "aaaaa11111")
        },
            onSuccess = {
                LogUtil.i("NetworkViewModel", "commonLog - onResume: success")
            },
            onResult = {
                LogUtil.i("NetworkViewModel", "commonLog - onResult: ${it.size}")
//                resultCode.set(it.size.toString())
            },
            onFailed = { code, msg ->
                LogUtil.i("NetworkViewModel", "commonLog - onFailed: $code, $msg")
            },
            onComplete = {
                dismissLoadingDialog()
            }
        )

    }
    val observableList = ObservableArrayList<Any>()

    val multipleItems = OnItemBindClass<Any>().apply {
        map<RvItemViewModel>(BR.viewModel, R.layout.rv_item)
        map<String>(BR.item, R.layout.item)
    }

    val modifyData = View.OnClickListener {
        observableList.set(page, "hezijie")
    }

    fun refresh(){
        observableList.clear()
        page = 1
        for (i in (50 * (page-1))..(50 * page)) {
            observableList.add("item $i")
            if (i % 2 == 0) {
                observableList.add(RvItemViewModel(this, "rv item $i"))
            }
        }
    }

    fun loadMore(){
        page ++;
        for (i in (50 * (page-1))..(50 * page)) {
            observableList.add("item $i")
            if (i % 2 == 0) {
                observableList.add(RvItemViewModel(this, "rv item $i"))
            }
        }
    }

    // 决定每个 item 占据屏幕的几列
    val spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
        override fun getSpanSize(position: Int): Int {
            if (observableList[position] is String) {
                return 2
            }
            return 6
        }
    }

//    val onRefresh = OnRefreshListener{
//        it.finishRefresh(2000)
//    }
//
//    val onLoadMore = OnLoadMoreListener{
//        it.finishLoadMore(2000)
//    }

    inner class OnRefreshAndLoadMore : OnRefreshLoadMoreListener{
        override fun onRefresh(refreshLayout: RefreshLayout) {
            refresh()
            refreshLayout.finishRefresh(2000)
        }

        override fun onLoadMore(refreshLayout: RefreshLayout) {
            loadMore()
            refreshLayout.finishLoadMore(2000)
        }

    }

    val refreshAndLoadMore = OnRefreshAndLoadMore()

//    object OnRefreshAndLoadMore:OnRefreshLoadMoreListener {
//        override fun onRefresh(refreshLayout: RefreshLayout) {
//            refreshLayout.finishRefresh(2000)
//        }
//
//        override fun onLoadMore(refreshLayout: RefreshLayout) {
//            refreshLayout.finishLoadMore(2000)
//        }
//
//    }
    var page = 1;
    init {

        for (i in 0..50) {
            observableList.add("item $i")
            if (i % 2 == 0) {
                observableList.add(RvItemViewModel(this, "rv item $i"))
            }
        }
    }

}